package github.kawaiior.yuanchuxiuzhen.core.init.event;

import github.kawaiior.yuanchuxiuzhen.Yuanchuxiuzhen;
import github.kawaiior.yuanchuxiuzhen.common.capability.IReplicableCap;
import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPowerProvider;
import github.kawaiior.yuanchuxiuzhen.common.item.armor.ManaStoneArmor;
import github.kawaiior.yuanchuxiuzhen.core.registry.ModCapability;
import github.kawaiior.yuanchuxiuzhen.network.NetworkRegistryHandler;
import github.kawaiior.yuanchuxiuzhen.network.packet.UpdateManaPacket;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.network.PacketDistributor;

@Mod.EventBusSubscriber
public class PowerEvent {

    @SubscribeEvent
    public static void onAttachEntityCapabilityEvent(AttachCapabilitiesEvent<Entity> event){
        // 实体能力
        Entity entity = event.getObject();
        if (entity instanceof PlayerEntity){
            Yuanchuxiuzhen.debug("为玩家添加Capability");
            event.addCapability(new ResourceLocation(Yuanchuxiuzhen.MODID,"ycxz_mana_power"),
                    new ManaPowerProvider());
        }
    }

    @SubscribeEvent
    public static void onAttachStackCapabilityEvent(AttachCapabilitiesEvent<ItemStack> event){
        // 物品堆能力
    }

    public static <T extends IReplicableCap> void handleCapOnPlayerClone(Capability<T> capability, PlayerEntity player, PlayerEntity original) {
        LazyOptional<T> cap = player.getCapability(capability);
        LazyOptional<T> originalCap = original.getCapability(capability);
        if (cap.isPresent() && originalCap.isPresent()){
            cap.ifPresent((c)->{
                originalCap.ifPresent(c::setPower);
            });
        }
    }

    @SubscribeEvent
    public static void onPlayerJoin(EntityJoinWorldEvent event){
        Entity entity = event.getEntity();
        if (event.getWorld().isRemote || !(entity instanceof PlayerEntity)){
            return;
        }
        PlayerEntity playerEntity = (PlayerEntity) entity;

        // 更新ManaPower
        ManaPower.update(playerEntity);
        // 向客户端发送ManaPower数据
        NetworkRegistryHandler.INSTANCE.send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) playerEntity),
                new UpdateManaPacket(playerEntity));
    }

    @SubscribeEvent
    public static void onPlayerClone(PlayerEvent.Clone event){
        PlayerEntity player = event.getPlayer();
        PlayerEntity original = event.getOriginal();

        if (!event.isWasDeath()){
            handleCapOnPlayerClone(ModCapability.MANA_POWER, player, original);
        }else {
            LazyOptional<ManaPower> originalCap = original.getCapability(ModCapability.MANA_POWER);
            originalCap.ifPresent((originalPower)->{
                if (originalPower.isVitality()){
                    originalPower.setVitality(false);
                    handleCapOnPlayerClone(ModCapability.MANA_POWER, player, original);
                    ManaPower.update(player);
                    player.setHealth(player.getMaxHealth());
                }
            });
        }

    }
}
